package handler

import (
	"log"

	product "git.oschina.net/fanbuchi/xgggh/product/proto/product"
	"golang.org/x/net/context"
	"git.oschina.net/fanbuchi/xgggh/product/service"
)

type Product struct {
   service.Products;
}

//Create micro query com.xgggh.srv.product Products.Create '{"name": "2分钱会员", "price": 2,"desc": "1分钱体验会员活动!","stock":10000,"type":1,"img":"https://img11.360buyimg.com/cms/jfs/t6991/69/866780876/6078/5a938446/597ab632Nd03a61f4.jpg","url":"https://item.jd.com/4551969.html?cpdad=1DLSUE"}'
func (handler *Product) Create(ctx context.Context, req *product.Product, rsp *product.CreateRsp) error {
	// oauth2Service
	err:=handler.Products.Create(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}

// Query micro query com.xgggh.srv.product Products.Query '{"id": 306454912159088641}'
func (handler *Product) Query(ctx context.Context, req *product.QueryReq, rsp *product.QueryRsp) error {
	product,err:=handler.Products.Query(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Product= product
	}
	return nil
}
//QueryList micro query com.xgggh.srv.product Products.QueryList '{"id":[306454912159088641]}'
func (handler *Product) QueryList(ctx context.Context, req *product.QueryListReq, rsp *product.QueryListRsp) error {
	products,err:=handler.Products.QueryList(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Product= products
	}
	return nil
}

//Update micro query com.xgggh.srv.product Products.Update '{"name": "1分钱会员", "price": 1,"desc": "大促销商品只要一分钱!","type":1,"img":"https://img11.360buyimg.com/cms/jfs/t6991/69/866780876/6078/5a938446/597ab632Nd03a61f4.jpg","url":"https://item.jd.com/4551969.html?cpdad=1DLSUE"}'
func (handler *Product) Update(ctx context.Context, req *product.Product, rsp *product.Response) error {
	// oauth2Service
	err:=handler.Products.Update(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}

// Delete micro query com.xgggh.srv.product Products.Delete '{"id": 306433967028994049}'
func (handler *Product) Delete(ctx context.Context, req *product.DeleteReq, rsp *product.Response) error {
	// oauth2Service
	err:=handler.Products.Delete(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
	}
	return nil
}



//Search micro query com.xgggh.srv.product Products.Search '{"page_number":1,"result_per_page":2,"params":{"name":"tiyanviper1"}}'
func (handler *Product) Search(ctx context.Context, req *product.SearchReq, rsp *product.SearchRsp) error {
	products,err:=handler.Products.Search(req)
	if err!=nil {
		rsp.ErrorDesc=err.Error()
	}else{
		rsp.Ok = true
		rsp.Product = products
	}
	return nil
}

// Stream is a server side stream handler called via client.Stream or the generated client code
func (handler *Product) Stream(ctx context.Context, req *product.StreamingRequest, stream product.Products_StreamStream) error {
	log.Printf("Received Example.Stream request with count: %d", req.Count)

	for i := 0; i < int(req.Count); i++ {
		log.Printf("Responding: %d", i)
		if err := stream.Send(&product.StreamingResponse{
			Count: int64(i),
		}); err != nil {
			return err
		}
	}

	return nil
}

// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (handler *Product) PingPong(ctx context.Context, stream product.Products_PingPongStream) error {
	for {
		req, err := stream.Recv()
		if err != nil {
			return err
		}
		log.Printf("Got ping %v", req.Stroke)
		if err := stream.Send(&product.Pong{Stroke: req.Stroke}); err != nil {
			return err
		}
	}
}
